package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.*;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdWildUserDataDaoImpl extends TempDataObjectDao<StrongholdWildUserData> {
    public static String getStrongholdWildUserDataSql = "select * from t_c_activity_stronghold_wild_user where activityId=?";

    public List<StrongholdWildUserData> getStrongholdWildUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<StrongholdWildUserData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getStrongholdWildUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateStrongholdWildUserDataSql = "update t_c_activity_stronghold_wild_user set wildId=?,patrons=?,rewardPoints=?,userBaseInfo=?,boxList=?,eventData=?,defendInfo=? where activityId=? and userId=? and serverId=?";

    @Override
    protected boolean updateData(StrongholdWildUserData strongholdWildUserData) {
        strongholdWildUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getWildId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(strongholdWildUserData.getPatrons())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getRewardPoints()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(strongholdWildUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdWildUserData.getBoxList())));
        para.put(i++, new DbParameter(Types.NVARCHAR, strongholdWildUserData.getEventData() == null ? "" : JSON.toJSONString(strongholdWildUserData.getEventData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdWildUserData.getDefendInfo().values())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildUserData.getServerId()));
        if (getDb().executeNoneQuery(updateStrongholdWildUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdWildUserDataSql = "insert into t_c_activity_stronghold_wild_user(activityId,userId,serverId,wildId,patrons,rewardPoints,userBaseInfo,boxList,eventData,defendInfo) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(StrongholdWildUserData strongholdWildUserData) {
        strongholdWildUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdWildUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getWildId()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(strongholdWildUserData.getPatrons())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdWildUserData.getRewardPoints()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(strongholdWildUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdWildUserData.getBoxList())));
        para.put(i++, new DbParameter(Types.NVARCHAR, strongholdWildUserData.getEventData() == null ? "" : JSON.toJSONString(strongholdWildUserData.getEventData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdWildUserData.getDefendInfo().values())));
        if (getDb().executeNoneQuery(insertStrongholdWildUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdWildUserData parseData(ResultSet rs) throws SQLException {
        StrongholdWildUserData strongholdWildUserData = new StrongholdWildUserData();
        strongholdWildUserData.setActivityId(rs.getInt("activityId"));
        strongholdWildUserData.setUserId(rs.getLong("userId"));
        strongholdWildUserData.setServerId(rs.getInt("serverId"));
        strongholdWildUserData.setWildId(rs.getInt("wildId"));
        strongholdWildUserData.setPatrons(JSON.parseObject(rs.getString("patrons"), UserPatrons.class));
        strongholdWildUserData.setRewardPoints(rs.getInt("rewardPoints"));
        strongholdWildUserData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        strongholdWildUserData.setBoxList(JSON.parseArray(rs.getString("boxList"), Integer.class));
        String eventStr = rs.getString("eventData");
        if (!StringUtils.isNullOrEmpty(eventStr)) {
            JSONObject eventData = JSON.parseObject(eventStr);
            Integer eventType = eventData.getInteger("type");
            StrongholdBaseEvent event = null;
            if (eventType == 1) {
                event = JSON.parseObject(eventStr, StrongholdNpcEvent.class);
            } else if (eventType == 2) {
                event = JSON.parseObject(eventStr, StrongholdPatronsEvent.class);
            } else if (eventType == 0) {
                event = JSON.parseObject(eventStr, StrongholdBoxEvent.class);
            }
            strongholdWildUserData.setEventData(event);
        }
        List<StrongholdDefendInfo> defendInfoList = JSON.parseArray(rs.getString("defendInfo"), StrongholdDefendInfo.class);
        Map<Long, StrongholdDefendInfo> infoMap = new ConcurrentHashMap<>();
        if (defendInfoList != null) {
            for (StrongholdDefendInfo info : defendInfoList) {
                infoMap.put(info.getUserId(), info);
            }
        }
        strongholdWildUserData.setDefendInfo(infoMap);
        return strongholdWildUserData;
    }

}
